<?php
class inviteControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$tpl=M("pagetpl")->get("invite","index");
		$this->smarty->display($tpl);
		$this->smarty->display("invite/index.html");
	}
	public function onMy(){
		$uids=M("invite")->selectCols(array("where"=>"userid=".M("login")->userid,"fields"=>"in_userid"));
		$data=M("user")->getUserByids($uids);
		  
		$this->smarty->goassign(array(
			"data"=>$data,
			"user"=>M("user")->selectRow("userid=".M("login")->userid),
			"invite"=>M("dataapi")->getword("邀请说明")
		));
		$tpl=M("pagetpl")->get("invite","my");
		$this->smarty->display($tpl);
		$this->smarty->display("invite/my.html");
	}
	
	public function onLog(){
		$uids=M("invite")->selectCols(array("where"=>"userid=".M("login")->userid,"fields"=>"in_userid"));
		$data=M("user")->getUserByids($uids);
		$this->smarty->goassign(array(
			"data"=>$data,
			"user"=>M("user")->selectRow("userid=".M("login")->userid)
		));
		$tpl=M("pagetpl")->get("invite","log");
		$this->smarty->display($tpl);
		$this->smarty->display("invite/log.html");
	}
	public function onReg(){
		$userid=get('userid','i');
		if($userid){
			setcookie("invite_uid",$userid,time()+3600*24,"/",DOMAIN);
		}
		header("Location: ".R("/index.php?m=register"));
	}
	
	public function onEWM(){
		if(INWEIXIN){
			$key="invite_ewm_".M("login")->userid;
			if(!$tk=cache()->get($key)){
				$data=get_weixin_access_token();
				$scene_id=1000000000+M("login")->userid;
				$ts=curl_post_json("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$data['access_token'],'{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}');
				$tk=json_decode($ts,true);
				cache()->set($key,$tk,2591990);
			}
			header("Content-type:image/png");
			//print_r($tk);
			echo curl_get_contents("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$tk['ticket']);
			
		}else{
			$this->loadClass("qrcode");
			$this->qrcode->png("http://".DOMAIN."/index.php?m=invite&a=reg&userid=".M("login")->userid);
		}
	}
	
	
}
?>